package com.cx.controller;

import cn.hutool.core.lang.UUID;
import com.cx.service.ProjectService;
import com.cx.service.TaskService;
import com.cx.util.AliOssUtil;
import com.cx.util.ResultUtil;
import com.cx.vo.CommonVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/common")
@Api(tags = "通用接口")
@Slf4j
@RequiredArgsConstructor
public class CommonController {

    private final AliOssUtil aliOssUtil;
    private final ProjectService projectService;
    private final TaskService taskService;


    @PostMapping("/upload")
    @ApiOperation(value = "文件上传")
    public ResultUtil<String> upload(@RequestBody MultipartFile file) throws IOException {
        log.info("文件上传:{}", file);
        String suffix = file.getOriginalFilename().split("\\.")[1];
        String prefix = UUID.randomUUID().toString(true);
        String newFileName = prefix + "." + suffix;
        // 上传到OSS
        String url = aliOssUtil.upload(file.getBytes(), newFileName);
        return ResultUtil.success(url);
    }

    @GetMapping("/selectUserOfProject")
    @ApiOperation(value = "查询项目经理所分配到的项目")
    public ResultUtil<List<CommonVO>> selectUserOfProject(){
        log.info("查询项目经理所分配到的项目");
        return projectService.selectUserOfProject();
    }

    @GetMapping("/selectProjectOfTask")
    @ApiOperation(value = "查询项目所分配到的任务")
    public ResultUtil<List<CommonVO>> selectProjectOfTask(){
        log.info("查询项目所分配到的任务");
        return taskService.selectProjectOfTask();
    }
}
